<?php
/**
 * Created by PhpStorm.
 * User: liuwei
 * Date: 2017/5/15
 * Time: 下午2:39
 */

class Model_User extends PhalApi_Model_NotORM {

    function getUserInfo($userId) {
        $rs =  DI()->notorm->user->select('username,nickname,email,register_stamp')->where('id',$userId)->fetch();
        // var_dump($rs);exit;
        $rs['register_datetime'] = date('Y-m-d H:i:s', $rs['register_stamp']);
        return $rs;
    }

    function checkUsername($username){
        return DI()->notorm->user->where('username',$username)->fetch();
    }

    function checkNickname($nickname) {
        return DI()->notorm->user->where('nickname',$nickname)->fetch();
    }

    function updateNickName($userId, $nickname) {
        $where = ['id' => $userId];
        $data = ['nickname' => $nickname];
        $rs = DI()->notorm->user->where($where)->update($data);
        return ($rs == 0 || $rs === false)? false : true;
    }

    public function register($username,$password)
    {
        $passwordCode = buildRandomString(3, 4);
        $password = md5($password . $passwordCode);
        $data = array(
            'username' => $username,
            'nickname'=>$username,
            'password_code' => $passwordCode,
            'password' => $password,
            'register_stamp'=>time(),
        );
        return DI()->notorm->user->insert($data);
    }

}
